<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="/context/mytags.jsp" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>关单</title>
    <t:base type="jquery,easyui,tools,DatePicker"></t:base>
    <script type="text/javascript" src="plug-in/ckfinder/ckfinder.js"></script>
    <link rel="stylesheet" href="plug-in/uploadify/css/uploadify.css" type="text/css"/>
    <script type="text/javascript" src="plug-in/uploadify/jquery.uploadify-3.1.min.js"></script>
</head>
<body>
<t:formvalid formid="formobj" action="" dialog="false" usePlugin="password" layout="table" tiptype="3"
             refresh="false">
    <input id="sessionId" type="hidden" value="${pageContext.session.id}"/>
    <table class="nobordertable noborder" style="border: none;">
        <input type="hidden" value="${id}" name="cbsId" id="cbsId">
        <tbody>
        <tr>
            <td class="al_r" width="30%" valign="top">
                <label>处理描述:</label>
            </td>
            <td width="70%">
                <textarea style="width: 100%; height: 80px;" id="description" maxlength="500"
                          datatype="*1-500" nullMsg="请填写处理描述"></textarea>
            </td>
        </tr>
        <tr>
            <td class="al_r" width="30%" valign="top">
                <label>附件:</label>
            </td>
            <td width="70%">
                <input type="file" id="remarksFiles" value="上传附件"/><span></span>
                <span id="fileName1"></span>
            </td>
        </tr>
        <tr>
            <td colspan="2" style="text-align: center;">
                <a href="#" onclick="sureCloseService()" class="easyui-linkbutton l-btn">确认</a>
            </td>
        </tr>
        </tbody>
    </table>
</t:formvalid>
</body>
<script>
    $("#formobj").Validform({
        ignoreHidden: true,
        postonce: true,
        showAllError: true,
        ajaxPost: true,
        tiptype: 3,
        beforeSubmit: function () {
            sureCloseService();
        }
    });

    var form;
    $(function () {
        form = $("#formobj").Validform({
            ignoreHidden: true,
            postonce: true,
            showAllError: true,
            closeOnEscape:false,
            ajaxPost: true,
            tiptype: 3,
        });
        $("#remarksFiles").uploadify({
            buttonText: '上传文件',
            auto: false,
            progressData: 'speed',
            multi: true,
            height: 20,
            width: 60,
            overrideEvents: ['onDialogClose'],
            fileTypeDesc: '文件格式:',
            queueID: 'instructionfile',
            fileTypeExts: '*', //控制可上传文件的扩展名，启用本项时需同时声明fileDesc
            fileSizeLimit: '50MB',
            swf: 'plug-in/uploadify/uploadify.swf',
            uploader: 'fileUploadController.do?uploadFile&businessKey='+$("#cbsId").val(),
            onSelect:function (file) {
                var html = '<div name="fileId" id="att_'+ file.id +'">' +
                    '<input type="hidden" id="fileIdCloseBill" value="' + file.name + '">' +
                    '<span class="color-blue">' + file.name + '</span>&nbsp;&nbsp; ' +
                    '<span style="cursor: pointer;" onclick=\'deleteUnUPloadFiles("'+ file.id +'")\'>[删除]</span>' +
                    '</div>';
                $("#remarksFiles").after(html);
            },onFallback: function () {
                tip("您未安装FLASH控件，无法上传文件！请安装FLASH控件后再试");
            }, onSelectError: function (file, errorCode, errorMsg) {
                switch (errorCode) {
                    case -100:
                        tip("上传的文件数量已经超出系统限制的" + $('#instruction').uploadify('settings', 'queueSizeLimit') + "个文件！");
                        break;
                    case -110:
                        tip("文件 [" + file.name + "] 大小超出系统限制的50MB大小！");
                        break;
                    case -120:
                        tip("文件 [" + file.name + "] 大小异常！");
                        break;
                    case -130:
                        tip("文件 [" + file.name + "] 类型不正确！");
                        break;
                }
            },onUploadStart:function(){
                lockmask(true, '请稍等,导入中...');
            },onUploadSuccess :function(){
                lockmask(false);
                tip("上传成功");
            },onQueueComplete :function(){
                saveCloseService();
            }
        });
    });
    /**
     * @Author Sunliangliang 2018/10/17 11:06
     * @Description 删除附件div 及uploadify文件队列中的文件
     * @params uploadify文件id
     */
    function deleteUnUPloadFiles(attFileId){
        delete $("#remarksFiles").data('uploadify').queueData.files[attFileId];
        $('#att_' + attFileId).remove();
    }

    function sureCloseService() {
        //调用formValidata 进行验证，并能阻止提交
        if (!form.check(false)) {
            form.submitForm(false);
            return false;
        }
        if ($("#description").val() == '') {
            tip("请填写关单备注");
            return;
        }

        /**
         * 如果 关单附件中没有上传文件 则直接执行保存方法，否则 在上传结束后执行保存（只针对于 关单页面中有一个附件上传，多个需要改逻辑）
         */
        var remarksFilesQueueLength = $("#remarksFiles").data('uploadify').queueData.queueLength;
        if(remarksFilesQueueLength > 0){
            $("#remarksFiles").uploadify('upload', '*');
        }else{
            saveCloseService();
        }
    }

    /**
     * 保存 关单的内容
     */
    function saveCloseService(){
        $.ajax({
            url: "cbsServiceController.do?closeServiceList",
            data: {
                "id": $("#cbsId").val(),
                "description": $("#description").val()
            },
            cache: false,
            dataType: 'json',
            type: 'POST',
            async: false,
            success: function (data) {
                if (data.success) {
                    parent.window.tip(data.msg);
                    window.top.$("#maintabs").tabs({
                        title: "编辑服务单" + data.obj,
                        onBeforeClose: function (title, index) {
                        }
                    });
                    window.top.$("#maintabs").tabs('close', "编辑服务单" + data.obj);
                    addOneTab("服务单管理", "cbsServiceController.do?cbsService");
                    frameElement.api.close();
                }else{
                    tip(data.msg)
                }
            }
        });
    }
</script>
</html>

